<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input ref='input' :placeholder="placeHolder" v-model="query" class="box" >
    <i class="icon-dismiss" v-show="query" @click="delQuery"></i>
  </div>
</template>

<script type="text/ecmascript-6">
//节流函数 
import {debounce} from 'common/js/util'
  export default {
   props: {
     placeHolder: {
      type: String,
      default: '请输入歌曲、歌手'
     }
   },
   data() {
     return {
       query: ''
     }
   },
   methods: {
     // 删除输入框text
     delQuery() {
       this.query= ''
     },
     // 接收父组件传递过来的 热门query
     setQuery(query) {
       this.query=query
     },
     
    // 文本框失去焦点
     blur() {
       this.$refs.input.blur()
     }
   },
   created() {
     this.$watch('query',debounce(newQuery=>{
       this.$emit('query',newQuery)
     },200))
   },
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .search-box
    display: flex
    align-items: center
    box-sizing: border-box
    width: 100%
    padding: 0 6px
    height: 40px
    background: $color-highlight-background
    border-radius: 6px
    .icon-search
      font-size: 24px
      color: $color-background
    .box
      flex: 1
      margin: 0 5px
      line-height: 18px
      background: $color-highlight-background
      color: $color-text
      font-size: $font-size-medium
      &::placeholder
        color: $color-text-d
    .icon-dismiss
      font-size: 16px
      color: $color-background
</style>